"""Regression model with exponential forgetting"""

import numpy as np
import matplotlib.pyplot as plt
from giw import *
from regmodel import *

class RegmodelEF(Regmodel, Giw):
    """
    Bayesian regression model with exponential forgetting

    Attributes
    ----------
    factor : float
        Forgetting factor
    
    Methods
    -------
    model_update(data, factor=None)
        Model_update model pdf with data and perform forgetting
    """

    def __init__(self, mat_v=None, nu=1, desc=None, factor=1, \
                 mixtools_compat=False):
        """Constructor"""
        super(RegmodelEF, self).__init__(mat_v, nu, desc, mixtools_compat)
        self.factor = factor

    def model_update(self, data, factor=None):
        """
        Update of the model

        Parameters
        ----------
        factor : float
            Forgetting factor. If set, it is stored to self.factor.
            If None, self.factor is used by default.
        """
        if factor != None:
            self.factor = factor

        prior_log_likelihood = self.log_likelihood

        self._v._d *= self.factor
        self._nu *= self.factor

        self.update(data)
        self._log['est_theta_hist'].append(self.est_theta)

        posterior_log_likelihood = self.log_likelihood
        self.delta_log_likelihood = posterior_log_likelihood - prior_log_likelihood
        self._model_log_likelihood += self.delta_log_likelihood

    #@property
    #def model_log_likelihood(self):
    #    return self._model_log_likelihood

#--------------------------------------------

if __name__ == "__main__":
    data_real = [7.]
    theta = (0.9, -0.5)
    for i in xrange(1, 100):
        data_real.append(theta[0] * data_real[i-1] \
                         + theta[1] + np.random.randn(1) * 0. + i/10)
    datamatrix = data_matrix(data_real, 1)
    data = iter(datamatrix)

    model = RegmodelEF(np.eye(3) * 0.01, factor=1., mixtools_compat=True)
    ll_prior = model.log_likelihood

    for dt in data:
        model.model_update(dt, 0.99)
    ll_post = model.log_likelihood


    print model.v.ld

    print "kumulativne", model._model_log_likelihood
    print "post - prior", ll_post - ll_prior
#    print "rodic", model.Regmodel_model_log_likelihood
#    print "potomek", model._RegmodelEF_model_log_likelihood

    model.plot_est_theta_hist()


    
